package com.lht.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lht.admin.constants.DataValidConstant;
import com.lht.admin.dao.OrderDao;
import com.lht.admin.entity.OrderContractEntity;
import com.lht.admin.entity.OrderEntity;
import com.lht.admin.entity.OrderFollowEntity;
import com.lht.admin.service.OrderContractService;
import com.lht.admin.service.OrderFollowService;
import com.lht.admin.service.OrderService;
import com.lht.admin.utils.ListUtil;
import com.lht.admin.utils.QueryPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单表Service实现类
 *
 * @author ZhengZhanWu
 * @date 2020-02-06 13:42:45
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Resource
    private OrderContractService orderContractService;
    @Resource
    private OrderFollowService orderFollowService;

    @Override
    public OrderEntity queryById(Long id) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("id", id);
        map.put("is_delete", DataValidConstant.IS_DELETE_FALSE_STR);
        OrderEntity order = null;
        List<OrderEntity> orderEntities = queryAll(map);
        for (OrderEntity dest : orderEntities) {
            if (id.equals(dest.getId())) {
                order = dest;
                break;
            }
        }
        if (null != order) {
            loadOrderContract(order, order.getApplyNo());
            loadOrderFollow(order, order.getId());
        }

        return order;
    }

    private void loadOrderFollow(OrderEntity order, Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<OrderFollowEntity> list = orderFollowService.queryAll(params);
        order.setOrderFollowList(list);
    }

    private void loadOrderContract(OrderEntity order, String applyNo) {
        OrderContractEntity orderContractEntity = ListUtil.getFirstElement(orderContractService.list(buildApplyNoQueryWrapper(applyNo)));
        order.setOrderContract(orderContractEntity);
    }

    private <T> QueryWrapper<T> buildApplyNoQueryWrapper(String applyNo) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("apply_no", applyNo);
        queryWrapper.eq("is_delete", DataValidConstant.IS_DELETE_FALSE_STR);

        return queryWrapper;
    }

    private <T> QueryWrapper<T> buildOrderIdQueryWrapper(Long id) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("is_delete", DataValidConstant.IS_DELETE_FALSE_STR);

        return queryWrapper;
    }

    @Override
    public List<OrderEntity> queryAll(Map<String, Object> params) {
        List<OrderEntity> orderEntities = baseMapper.queryAll(params);
        return orderEntities;
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<OrderEntity> page = new QueryPlus<OrderEntity>(params).getPage();
        return page.setRecords(baseMapper.selectOrderPage(page, params));
    }

    @Override
    public boolean add(OrderEntity order) {
        return this.save(order);
    }

    @Override
    public boolean update(OrderEntity order) {
        return this.updateById(order);
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
}
